package com.ldy.frame;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import static com.ldy.frame.PFrame.TextImfo;


/**
 * Created by MacBook Pro on 2017/11/28.
 */
public class PImgPanel extends JPanel{
    String ImagePath ="C:\\Users\\MacBook Pro\\Desktop\\娱乐\\2.jpg";
    public  PImgPanel()
    {

        //设计为图片的Panel。
        //生成图片/
        ImageIcon icon =new ImageIcon(ImagePath);
        //通过设置图片大小，来修改按钮大小
        icon.setImage(icon.getImage().getScaledInstance(240,366, Image.SCALE_DEFAULT));
        //将图片Panel设置为两行三列的网格形状
        this.setLayout(new GridLayout(2,3));
        //创建一行一列按钮，将图片设置为按钮背景，并添加事件
        JButton jb1 =new JButton(icon);
        //将按钮设施为透明，并且无边框以便图片填充整个按钮
        jb1.setOpaque(false);
        jb1.setMargin(new Insets(0,0,0,0));
        jb1.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                System.out.println("hello world!    1");
                TextImfo.ta.setText("");
                TextImfo.ta.append("hello world!    1");

            }
        });

        JButton jb2 =new JButton(icon);
        jb2.setOpaque(false);
        jb2.setMargin(new Insets(0,0,0,0));
        jb2.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                System.out.println("hello world!    2");
                TextImfo.ta.setText("");
                TextImfo.ta.append("hello world!    2");
            }
        });


        JButton jb3 =new JButton(icon);
        jb3.setOpaque(false);
        jb3.setMargin(new Insets(0,0,0,0));
        jb3.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                System.out.println("hello world!    3");
                TextImfo.ta.setText("");
                TextImfo.ta.append("hello world!    3");
            }
        });


        JButton jb4 =new JButton(icon);
        jb4.setOpaque(false);
        jb4.setMargin(new Insets(0,0,0,0));
        jb4.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                System.out.println("hello world!    4");
                TextImfo.ta.setText("");
                TextImfo.ta.append("hello world!    4");
            }
        });




        JButton jb5 =new JButton(icon);
        jb5.setOpaque(false);
        jb5.setMargin(new Insets(0,0,0,0));
        jb5.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                System.out.println("hello world!    5");
                TextImfo.ta.setText("");
                TextImfo.ta.append("hello world!    5");
            }
        });



        JButton jb6 =new JButton(icon);
        jb6.setOpaque(false);
        jb6.setMargin(new Insets(0,0,0,0));
        jb6.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                System.out.println("hello world!    6");
                TextImfo.ta.setText("");
                TextImfo.ta.append("hello world!    6");

            }
        });

        //将创建的按钮添加至Panel中
        this.add(jb1);
        this.add(jb2);
        this.add(jb3);
        this.add(jb4);
        this.add(jb5);
        this.add(jb6);

    }

    public static void main(String[] args) {
        JFrame jf =new JFrame("图片测试");
        jf.add(new PImgPanel());
        jf.setVisible(true);
        jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        jf.setSize(500,500);
    }

}














/*
class PImageDraw extends JPanel

{
    String  FilePath;
    public void PImageDraw(String Path)
    {
        FilePath =Path;
    }

    protected void paintComponent(Graphics g){
        try{
            File fl =new File(FilePath);
            BufferedImage img1=ImageIO.read(fl);
            //BufferedImage img = ImageIO.read(new File(this.getClass().getResource("Winter.jpg").getPath()));
            g.drawImage(img1,0,0,240,336,null);
            /*
            BufferedImage img2 =ImageIO.read(new File("C:\\Users\\MacBook Pro\\Desktop\\娱乐\\2.jpg"));
            g.drawImage(img1,241,0,240,336,null);
            BufferedImage img3 =ImageIO.read(new File("C:\\Users\\MacBook Pro\\Desktop\\娱乐\\2.jpg"));
            g.drawImage(img1,483,0,240,336,null);
            BufferedImage img4 =ImageIO.read(new File("C:\\Users\\MacBook Pro\\Desktop\\娱乐\\2.jpg"));
            g.drawImage(img1,0,338,240,336,null);
            BufferedImage img5 =ImageIO.read(new File("C:\\Users\\MacBook Pro\\Desktop\\娱乐\\2.jpg"));
            g.drawImage(img1,241,338,240,336,null);
            BufferedImage img6 =ImageIO.read(new File("C:\\Users\\MacBook Pro\\Desktop\\娱乐\\2.jpg"));
            g.drawImage(img1,483,338,240,336,null);

        }catch(IOException e){
            e.printStackTrace();
        }
    }
}
*/
